from .import constants
from celery_tasks.main import celery_app
from celery_tasks.sms.yuntongxun.ccp_sms import CCP
import logging

logger = logging.getLogger("django")


# 发送短信任务
@celery_app.task(bind=True, name="send_sms_code", retry_backoff=3)
def send_sms_code(self, mobile, sms_code):
    try:
        send_value = CCP().sendTemplateSMS(mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES // 6], constants.SEND_SMS_TEMPLATE_ID)
    except Exception as e:
        logger.error(e)
        raise self.retry(exc=e, max_retries=3)
    if send_value != 0:
        raise self.retry(exc=Exception("发送短信失败"), max_retries=3)
    return send_value